<?php

echo '<link rel="stylesheet" type="text/css" href="css/style.css" />';
installDatabase();

function installDatabase() {
    include_once '../db-control/inc/dbInfo.php';
    $connected = mysql_connect($host, $username, $password);

    if (!$connected) {
        return mysql_error();
    }

    $result = mysql_select_db($database);
    if (isset($_POST['submitPass'])) {
        $pass = $_POST['pass'];
        if ($pass == 'password') {
            //do nothing, password is correct, continue 
        } else {
            die("<font color='red'>Incorrect Password!</font>");
        }
    } else {
        if ($result

            )die("<h1>Database already exists!</h1><br /><p>Type the password to delete the database and install anyway.</p><form action='index.php' method='post'><input type='password' name='pass' /><br /><br /><input type='submit' class='submit' value='Install' name='submitPass' /></form>");
        //password = 'password';
    }

    mysql_query("drop database $database");
    mysql_query("CREATE USER '$database'@'localhost' IDENTIFIED BY 'root';");
    mysql_query("GRANT ALL PRIVILEGES ON * . * TO '$database'@'localhost';");
    mysql_query("IDENTIFIED BY 'root' WITH GRANT OPTION");
    mysql_query("MAX_QUERIES_PER_HOUR 0");
    mysql_query("MAX_CONNECTIONS_PER_HOUR 0");
    mysql_query("MAX_UPDATES_PER_HOUR");
    mysql_query("MAX_USER_CONNECTIONS 0;");
    mysql_query("create database if not exists $database") or die("Could not create database" . mysql_error());
    mysql_query("GRANT ALL PRIVILEGES ON '$database' . * TO '$database'@'localhost':");
    mysql_select_db($database);



    mysql_query("create table organization(
id                      int             NOT NULL AUTO_INCREMENT PRIMARY KEY,
name                    varchar(45)     NOT NULL,
description             text            NULL,
address                 varchar(45)     NOT NULL,
address1                varchar(45)     NOT NULL,
city                    varchar(25)     NOT NULL,
state                   varchar(2)      NOT NULL,
zip                     varchar(10)	NOT NULL
);");

    mysql_query("create table contact(
id                      int             NOT NULL AUTO_INCREMENT PRIMARY KEY,
organizationID          int             NOT NULL,
fname                   varchar(20)     NOT NULL,
lname                   varchar(20)     NOT NULL,
email                   varchar(50)	NULL,
phone                   varchar(13)	NULL,
ext                     varchar(4)      NULL,
fax		        varchar(13)     NULL
);");

    mysql_query("create table opportunity(
opportunityID		int	NOT NULL AUTO_INCREMENT PRIMARY KEY,
organizationID		int		NOT NULL,
contactID		int		NOT NULL,
title			varchar(25)	NOT NULL,
numPositionsAvailable	int		NOT NULL,
semester		varchar(20)	NOT NULL,
payRate			decimal(3,2)	NOT NULL,
duration		varchar(15)	NOT NULL,
responsibilities	text		NOT NULL,
qualifications		text		NULL,
instructions		text		NULL,
comments                text            NULL,
categories              text            NULL,
dateAdded		date		NOT NULL
);");
    mysql_query("create table internship(
id            int		NOT NULL AUTO_INCREMENT PRIMARY KEY,
opportunityID           int		NOT NULL,
studentID		int		NOT NULL,
gotHired		varchar(25)	NOT NULL,
hours			varchar(25)	NOT NULL,
special			varchar(3)	NOT NULL,
semester		varchar(10)	NOT NULL
);");
    mysql_query("create table student(
id                      int		NOT NULL AUTO_INCREMENT PRIMARY KEY,
fname                   varchar(25)  	NOT NULL,
lname                   varchar(25)	NOT NULL,
username		varchar(10)	NOT NULL,
currentGPA		decimal(2,2)	NOT NULL,
major			text		NOT NULL,
yearStatus		text		NULL,
dateModified            date		NOT NULL
);");

    mysql_query("create table category(
name    varchar(45) NOT NULL PRIMARY KEY
);
");

    mysql_query("create table major(
name    varchar(45) NOT NULL PRIMARY KEY
);
");



    mysql_query("INSERT INTO major VALUES('Biology')");
    mysql_query("INSERT INTO major VALUES('CA')");
    mysql_query("INSERT INTO major VALUES('CIS')");
    mysql_query("INSERT INTO major VALUES('English')");
    mysql_query("INSERT INTO major VALUES('History')");
    mysql_query("INSERT INTO major VALUES('PS')");
    mysql_query("INSERT INTO major VALUES('Psyc')");
    mysql_query("INSERT INTO major VALUES('Unknown')");

    mysql_query("INSERT INTO category VALUES('Advertising/Graphic Design')");
    mysql_query("INSERT INTO category VALUES('CIS Related')");
    mysql_query("INSERT INTO category VALUES('Communications')");
    mysql_query("INSERT INTO category VALUES('Community Service')");
    mysql_query("INSERT INTO category VALUES('Educational')");
    mysql_query("INSERT INTO category VALUES('Event Planning')");
    mysql_query("INSERT INTO category VALUES('Finance/Economics')");
    mysql_query("INSERT INTO category VALUES('Government/Political')");
    mysql_query("INSERT INTO category VALUES('Health & Human Services')");
    mysql_query("INSERT INTO category VALUES('Legal')");
    mysql_query("INSERT INTO category VALUES('Marketing/PR')");
    mysql_query("INSERT INTO category VALUES('Psychology')");
    mysql_query("INSERT INTO category VALUES('Research')");
    mysql_query("INSERT INTO category VALUES('Sales')");
    mysql_query("INSERT INTO category VALUES('Technology')");
    mysql_query("INSERT INTO category VALUES('Various')");
    mysql_query("INSERT INTO category VALUES('Web/Information Technology')");
    mysql_query("INSERT INTO category VALUES('Writing/Journalism')");


    /* mysql_query("ALTER TABLE internship_opportunity ADD CONSTRAINT FOREIGN KEY intern_op_fk (organizationID)
      REFERENCES organization (organizationID);");
      mysql_query("ALTER TABLE INTERNSHIP ADD CONSTRAINT FOREIGN KEY internship_fk1 (opportunityID)
      REFERENCES internship_opportunity (opportunityID);");
      mysql_query("ALTER TABLE INTERNSHIP ADD CONSTRAINT FOREIGN KEY internship_fk2 (studentID)
      REFERENCES STUDENT (studentID);");
      mysql_query("ALTER TABLE CONTACTS ADD CONSTRAINT FOREIGN KEY contact_fk (organizationID)
      REFERENCES organization (organizationID);"); */


    mysql_query("create table user(
    id                  int             NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username		varchar(25)	NOT NULL,
    password		varchar(32)	NOT NULL,
    fname		varchar(25)	NOT NULL,
    lname		varchar(25)	NOT NULL,
    permissionLevel     text		NOT NULL
    );");
}

echo "<div align='left'>";
echo "<h2>Installation Successful!</br></h2>";
echo "<hr />";
echo "</div>";
?>
